package com.example;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class TaxCalculator {
    public static TaxCalculation calculate(TaxCalculation taxInput) {
        KieSession kieSession = null;
        try {
            System.out.println("1. 初始化KieServices");
            KieServices kieServices = KieServices.Factory.get();

            System.out.println("2. 加载KieContainer");
            KieContainer kieContainer = kieServices.getKieClasspathContainer();

            System.out.println("3. 创建KieSession");
            kieSession = kieContainer.newKieSession("taxSession");

            System.out.println("4. 插入事实对象: " + taxInput);
            kieSession.insert(taxInput);

            System.out.println("5. 执行规则");
            int fired = kieSession.fireAllRules();
            System.out.println("6. 触发了 " + fired + " 条规则");

            return taxInput;
        } catch (Exception e) {
            System.err.println("发生错误: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("计算失败", e);
        } finally {
            if (kieSession != null) {
                kieSession.dispose();
                System.out.println("7. 释放KieSession资源");
            }
        }
    }
}